<?php
/**
 * 权限资源分类
 * @return array
 */
function getRoleMenu(){
    return  array('system'=>'系统设置','admin'=>'权限管理','agent'=>'代理管理','user'=>'会员中心','order'=>'订单中心','article'=>'营销推广','goods'=>'商品管理','shop'=>'商户管理','count'=>'统计报表'
    );
}

function getMenuArr(){
    $menuArr = include APP_PATH.'admin/config/menu.php';
    $act_list = session('act_list');
    if($act_list != 'all' && !empty($act_list)){
        $right = M('system_menu')->where("id in ($act_list)")->cache(true)->getField('right',true);
        foreach ($right as $val){
            $role_right .= $val.',';
        }
        foreach($menuArr as $k=>$val){
            foreach ($val['child'] as $j=>$v){
                foreach ($v['child'] as $s=>$son){
                    if(strpos($role_right,$son['op'].'@'.$son['act']) === false){
                        unset($menuArr[$k]['child'][$j]['child'][$s]);//过滤菜单
                    }
                }
            }
        }
        foreach ($menuArr as $mk=>$mr){
            foreach ($mr['child'] as $nk=>$nrr){
                if(empty($nrr['child'])){
                    unset($menuArr[$mk]['child'][$nk]);
                }
            }
        }
    }
    return $menuArr;
}


function getAllMenu(){
    return	array(
        'system' => array('name'=>'系统设置','icon'=>'fa-cog','sub_menu'=>array(
            array('name'=>'网站设置','act'=>'index','control'=>'System'),
            array('name'=>'友情链接','act'=>'linkList','control'=>'Article'),
            array('name'=>'自定义导航','act'=>'navigationList','control'=>'System'),
            array('name'=>'区域管理','act'=>'region','control'=>'Tools'),
            array('name'=>'短信模板','act'=>'index','control'=>'SmsTemplate'),

        )),
        'access' => array('name' => '权限管理', 'icon'=>'fa-gears', 'sub_menu' => array(
            array('name'=>'权限资源列表','act'=>'right_list','control'=>'System'),
            array('name' => '管理员列表', 'act'=>'index', 'control'=>'Admin'),
            array('name' => '角色管理', 'act'=>'role', 'control'=>'Admin'),
            array('name' => '供应商管理', 'act'=>'supplier', 'control'=>'Admin'),
            array('name' => '管理员日志', 'act'=>'log', 'control'=>'Admin'),
        )),
        'member' => array('name'=>'会员管理','icon'=>'fa-user','sub_menu'=>array(
            array('name'=>'会员列表','act'=>'index','control'=>'User'),
            array('name'=>'会员等级','act'=>'levelList','control'=>'User'),
            array('name'=>'充值记录','act'=>'recharge','control'=>'User'),
            array('name' => '提现申请', 'act'=>'withdrawals', 'control'=>'User'),
            array('name' => '汇款记录', 'act'=>'remittance', 'control'=>'User'),
            //array('name'=>'会员整合','act'=>'integrate','control'=>'User'),
        )),
        'goods' => array('name' => '商品管理', 'icon'=>'fa-book', 'sub_menu' => array(
            array('name' => '商品分类', 'act'=>'categoryList', 'control'=>'Goods'),
            array('name' => '商品列表', 'act'=>'goodsList', 'control'=>'Goods'),
            array('name' => '商品模型', 'act'=>'goodsTypeList', 'control'=>'Goods'),
            array('name' => '商品规格', 'act' =>'specList', 'control' => 'Goods'),
            array('name' => '商品属性', 'act'=>'goodsAttributeList', 'control'=>'Goods'),
            array('name' => '品牌列表', 'act'=>'brandList', 'control'=>'Goods'),
            array('name' => '商品评论','act'=>'index','control'=>'Comment'),
            array('name' => '商品咨询','act'=>'ask_list','control'=>'Comment'),
        )),
        'order' => array('name' => '订单管理', 'icon'=>'fa-money', 'sub_menu' => array(
            array('name' => '订单列表', 'act'=>'index', 'control'=>'Order'),
            array('name' => '发货单', 'act'=>'delivery_list', 'control'=>'Order'),
            //array('name' => '快递单', 'act'=>'express_list', 'control'=>'Order'),
            array('name' => '退货单', 'act'=>'return_list', 'control'=>'Order'),
            array('name' => '添加订单', 'act'=>'add_order', 'control'=>'Order'),
            array('name' => '订单日志', 'act'=>'order_log', 'control'=>'Order'),
        )),
        'promotion' => array('name' => '促销管理', 'icon'=>'fa-bell', 'sub_menu' => array(
            array('name' => '抢购管理', 'act'=>'flash_sale', 'control'=>'Promotion'),
            array('name' => '团购管理', 'act'=>'group_buy_list', 'control'=>'Promotion'),
            array('name' => '商品促销', 'act'=>'prom_goods_list', 'control'=>'Promotion'),
            array('name' => '订单促销', 'act'=>'prom_order_list', 'control'=>'Promotion'),
            array('name' => '代金券管理','act'=>'index', 'control'=>'Coupon'),
            array('name' => '预售管理','act'=>'pre_sell_list', 'control'=>'Promotion'),
        )),
        'Ad' => array('name' => '广告管理', 'icon'=>'fa-flag', 'sub_menu' => array(
            array('name' => '广告列表', 'act'=>'adList', 'control'=>'Ad'),
            array('name' => '广告位置', 'act'=>'positionList', 'control'=>'Ad'),
        )),
        'content' => array('name' => '内容管理', 'icon'=>'fa-comments', 'sub_menu' => array(
            array('name' => '文章列表', 'act'=>'articleList', 'control'=>'Article'),
            array('name' => '文章分类', 'act'=>'categoryList', 'control'=>'Article'),
            //array('name' => '帮助管理', 'act'=>'help_list', 'control'=>'Article'),
            //array('name' => '公告管理', 'act'=>'notice_list', 'control'=>'Article'),
            array('name' => '专题列表', 'act'=>'topicList', 'control'=>'Topic'),
        )),
        'weixin' => array('name' => '微信管理', 'icon'=>'fa-weixin', 'sub_menu' => array(
            array('name' => '公众号管理', 'act'=>'index', 'control'=>'Wechat'),
            array('name' => '微信菜单管理', 'act'=>'menu', 'control'=>'Wechat'),
            array('name' => '文本回复', 'act'=>'text', 'control'=>'Wechat'),
            array('name' => '图文回复', 'act'=>'img', 'control'=>'Wechat'),
            //array('name' => '组合回复', 'act'=>'nes', 'control'=>'Wechat'),
            //array('name' => '消息推送', 'act'=>'news', 'control'=>'Wechat'),
        )),
        'theme' => array('name' => '模板管理', 'icon'=>'fa-adjust', 'sub_menu' => array(
            array('name' => 'PC端模板', 'act'=>'templateList?t=pc', 'control'=>'Template'),
            array('name' => '手机端模板', 'act'=>'templateList?t=mobile', 'control'=>'Template'),
        )),

        'distribut' => array('name' => '分销管理', 'icon'=>'fa-cubes', 'sub_menu' => array(
            array('name' => '分销商品列表', 'act'=>'goods_list', 'control'=>'Distribut'),
            array('name' => '分销商列表', 'act'=>'distributor_list', 'control'=>'Distribut'),
            array('name' => '分销关系', 'act'=>'tree', 'control'=>'Distribut'),
            array('name' => '分销设置', 'act'=>'set', 'control'=>'Distribut'),
            array('name' => '分成日志', 'act'=>'rebate_log', 'control'=>'Distribut'),
        )),

        'tools' => array('name' => '插件工具', 'icon'=>'fa-plug', 'sub_menu' => array(
            array('name' => '插件列表', 'act'=>'index', 'control'=>'Plugin'),
            array('name' => '数据备份', 'act'=>'index', 'control'=>'Tools'),
            array('name' => '数据还原', 'act'=>'restore', 'control'=>'Tools'),
        )),
        'count' => array('name' => '统计报表', 'icon'=>'fa-signal', 'sub_menu' => array(
            array('name' => '销售概况', 'act'=>'index', 'control'=>'Report'),
            array('name' => '销售排行', 'act'=>'saleTop', 'control'=>'Report'),
            array('name' => '会员排行', 'act'=>'userTop', 'control'=>'Report'),
            array('name' => '销售明细', 'act'=>'saleList', 'control'=>'Report'),
            array('name' => '会员统计', 'act'=>'user', 'control'=>'Report'),
            array('name' => '财务统计', 'act'=>'finance', 'control'=>'Report'),
        )),
        'pickup' => array('name' => '自提点管理', 'icon'=>'fa-anchor', 'sub_menu' => array(
            array('name' => '自提点列表', 'act'=>'index', 'control'=>'Pickup'),
            array('name' => '添加自提点', 'act'=>'add', 'control'=>'Pickup'),
        ))
    );
}

/**
 * 获取缓存或者更新缓存
 * @param string $config_key 缓存文件名称
 * @param array $data 缓存数据  array('k1'=>'v1','k2'=>'v3')
 * @return array or string or bool
 */
function tpCache($config_key,$data = array()){
    $param = explode('.', $config_key);
    if(empty($data)){
        //缓存文件不存在就读取数据库
        $res = \think\Db::name('configs')->where("inc_type",$param[0])->select();
        if($res){
            foreach($res as $k=>$val){
                $config[$val['name']] = $val['value'];
            }
        }
        return $config;
    }else{
        //更新缓存
        $result = \think\Db::name('configs')->where('inc_type',$param[0])->select();
        if($result){
            foreach($result as $val){
                $temp[$val['name']] = $val['value'];
            }

            foreach($data as $k=>$v){
                $newArr = array('name'=>$k,'value'=>trim($v),'inc_type'=>$param[0]);
                if(!isset($temp[$k])){
                    \think\Db::name('configs')->insert($newArr);//新key数据插入数据库
                }else{
                    if($v!=$temp[$k]){
                        \think\Db::name('configs')->where('name',$k)->update($newArr);//缓存key存在且值有变更新此项
                    }
                }
            }

            //更新后的数据库记录
            $newRes = \think\Db::name('configs')->where("inc_type", $param[0])->select();
            foreach ($newRes as $rs){
                $newData[$rs['name']] = $rs['value'];
            }
        }else{
            foreach($data as $k=>$v){
                $newArr[] = array('name'=>$k,'value'=>trim($v),'inc_type'=>$param[0]);
            }
            \think\Db::name('configs')->insertAll($newArr);//插入configs数据

            $newData = $data;
        }
//        return $newData;
    }
}

